package com.njxxted.activiti.modules.process.extend.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.njxxted.activiti.common.util.FeignUtil;
import com.njxxted.activiti.common.util.PageUtil;
import com.njxxted.activiti.common.util.StringUtils;
import com.njxxted.activiti.common.util.UserUtil;
import com.njxxted.activiti.modules.api.PushApi;
import com.njxxted.activiti.modules.api.entity.PushVo;
import com.njxxted.activiti.modules.api.entity.UserDto;
import com.njxxted.activiti.modules.process.extend.dao.ExtendActUrgentDao;
import com.njxxted.activiti.modules.process.extend.entity.ExtendActUrgentEntity;
import com.njxxted.activiti.modules.process.extend.entity.req.ExtendActUrgentReqEntity;
import com.njxxted.activiti.modules.process.extend.service.ExtendActUrgentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 描述：流程催办service实现类
 * <p>
 * 作者：Ostrich Hu
 * 时间：2019/11/25 16:51 星期一
 */
@Service
public class ExtendActUrgentServiceImpl implements ExtendActUrgentService {

    @Resource
    private ExtendActUrgentDao extendActUrgentDao;

    @Override
    public ExtendActUrgentEntity queryObject(String id) {
        return extendActUrgentDao.queryObject(id);
    }

    @Override
    public PageUtil<ExtendActUrgentEntity> queryList(ExtendActUrgentReqEntity param) {
        if (Objects.isNull(param)) {
            return null;
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<ExtendActUrgentEntity> actOpinionEntityList = extendActUrgentDao.queryListByBean(param);
        PageInfo<ExtendActUrgentEntity> pageInfo = new PageInfo<>(actOpinionEntityList);
        return new PageUtil<>(actOpinionEntityList, (int) pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());

    }

    @Override
    public List<ExtendActUrgentEntity> queryListByBean(ExtendActUrgentReqEntity param) {
        return extendActUrgentDao.queryListByBean(param);
    }

    @Override
    public int saveOrUpdate(ExtendActUrgentReqEntity urgentReqEntity) {
        UserDto user = UserUtil.getUserFromRedis();

        urgentReqEntity.setUrgentUser(user.getId());
        urgentReqEntity.setUrgentTime(new Date());
        int save;
        if (StringUtils.isNotBlank(urgentReqEntity.getId())) {
            //修改催办信息
            save = extendActUrgentDao.update(urgentReqEntity);
        } else {
            //保存催办信息
            urgentReqEntity.setId(StringUtils.uuidGenerator());
            urgentReqEntity.setUrgentCount(1);
            save = extendActUrgentDao.save(urgentReqEntity);
        }
        if (StringUtils.isNotBlank(urgentReqEntity.getReceiver())) {
            String[] receivers = urgentReqEntity.getReceiver().split(",");
            PushVo pushVo = new PushVo();
            Map<String, Object> conditionMap = Maps.newHashMap();
            conditionMap.put("startedUser", user.getName());
            conditionMap.put("currNodeName", urgentReqEntity.getNodeName());
            Arrays.asList(receivers).parallelStream().forEachOrdered(uid -> {
                //调用消息推送催办信息
                pushVo.setId(StringUtils.uuidGenerator());
                pushVo.setBusinessId(urgentReqEntity.getId());
                pushVo.setAcceptClientId(uid);
                pushVo.setStartClientId(UserUtil.userId());
                pushVo.setDestination("/queue/specUser");
                pushVo.setConditionMap(JSON.toJSONString(conditionMap));
                pushVo.setTypes("urgent_notice");
                pushVo.setStatus(0);
                pushVo.setRemark("流程催办");
                //应用内推送
                if (Objects.equals(urgentReqEntity.getSendType(), 1)) {
                    new FeignUtil<PushApi>().invokeCommonService(PushApi.class).convertAndSendToUser(pushVo);
                } else {
                    //短信推送
                }

            });
        }
        return save;
    }

    @Override
    public int delete(String id) {
        return extendActUrgentDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return extendActUrgentDao.deleteBatch(ids);
    }
}
